Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
28 lines (24 loc) · 801 Bytes

3.4.4 - Coroutine/Http/Client->upgrade.md

File metadata and controls

28 lines (24 loc) · 801 Bytes

Coroutine\Http\Client->upgrade

升级为WebSocket连接。

function Coroutine\Http\Client->upgrade(string $path);
  • 请求失败返回false,成功返回true
  • 某些情况下请求虽然是成功的,upgrade返回了true,但服务器并未设置HTTP状态码为101,而是200403,这说明服务器拒绝了握手请求
  • WebSocket握手成功后可以使用push方法向服务器端推送消息,也可以调用recv接收消息
  • upgrade会产生一次协程调度

使用实例

go(function () {
    $cli = new Co\http\Client("127.0.0.1", 9501);
    $ret = $cli->upgrade("/");
    if ($ret) {
        while(true) {
            $cli->push("hello");
            var_dump($cli->recv());
            co::sleep(0.1);
        }
    }
});